package com.jy.datapipeline.importserver.controller;

import com.jy.datapipeline.common.support.EasyExcelSupport;
import com.jy.datapipeline.common.support.ExcelSupport;
import com.jy.datapipeline.common.constants.ExcelConstants;
import com.jy.datapipeline.common.constants.FormatConstants;
import com.jy.datapipeline.importserver.common.dto.TaskTemplateDto;
import com.jy.datapipeline.common.exception.GlobalException;
import com.jy.datapipeline.common.vo.ReturnVo;
import com.jy.datapipeline.importserver.entity.ImportTask;
import com.jy.datapipeline.importserver.service.ImportTaskService;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;

/**
 * @类名 ImportTaskController
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/4/18 19:06
 * @版本 1.0
 */
@RestController
@RequestMapping("/task")
public class ImportTaskController {

    private final ImportTaskService importTaskService;

    private final ExcelSupport excelSupport;

    private final EasyExcelSupport easyExcelSupport;

    public ImportTaskController(ImportTaskService importTaskService, ExcelSupport excelSupport, EasyExcelSupport easyExcelSupport) {
        this.importTaskService = importTaskService;
        this.excelSupport = excelSupport;
        this.easyExcelSupport = easyExcelSupport;
    }

    @GetMapping("/query-by-page")
    public ReturnVo queryByPage(@RequestParam(defaultValue = "1") Long pageNo, @RequestParam(defaultValue = "10") Long pageSize, String taskId,
                                String taskType, String targetDataSourceId, String storeId, String taskStatus, String ruleId, Integer trySubtaskLoadCount,
                                String latestStatus) throws InterruptedException {
        return importTaskService.queryByPage(pageNo, pageSize, taskId, taskType, targetDataSourceId, storeId, taskStatus, ruleId, trySubtaskLoadCount, latestStatus);
    }

    @GetMapping("/query-names")
    public ReturnVo queryAllNames(String taskName) {
        return ReturnVo.ok().put(importTaskService.queryNames(taskName));
    }

    @GetMapping("/query-by-id")
    public ReturnVo queryById(@RequestParam Long id) {
        ImportTask importTask = importTaskService.queryOneById(id);
        return ReturnVo.ok().put(importTask);
    }

    @PostMapping("/addition")
    public ReturnVo addTask(@RequestBody ImportTask importTask) throws GlobalException, SchedulerException {
        importTaskService.addTask(importTask);
        return ReturnVo.ok();
    }

    @PutMapping("/modification")
    public ReturnVo modifyTask(@RequestBody ImportTask importTask) throws GlobalException, SchedulerException {
        importTaskService.modifyTask(importTask);
        return ReturnVo.ok();
    }

    @DeleteMapping("/deletion")
    public ReturnVo deleteTask(@RequestParam Long[] ids) throws GlobalException, SchedulerException {
        importTaskService.deleteTask(ids);
        return ReturnVo.ok();
    }

    @PutMapping("/pause")
    public ReturnVo pauseTask(@RequestParam Long id) throws SchedulerException, GlobalException {
        importTaskService.pauseTask(id);
        return ReturnVo.ok();
    }

    /**
     * 启动自动任务
     * @param id
     * @return
     * @throws SchedulerException
     * @throws GlobalException
     */
    @PutMapping("/resume")
    public ReturnVo resumeTask(@RequestParam Long id) throws SchedulerException, GlobalException {
        importTaskService.resumeTask(Collections.singleton(id).toArray(new Long[0]));
        return ReturnVo.ok();
    }

    /**
     * 单次(或者批量执行手动任务)
     * @param taskIds
     * @return
     * @throws GlobalException
     */
    @PostMapping("/start")
    public ReturnVo handStart(@RequestParam String[] taskIds) throws GlobalException, ParseException, SchedulerException {
        for (String taskId : taskIds) {
            importTaskService.startTask(taskId);
        }
        return ReturnVo.ok();
    }

    @GetMapping("/download")
    public void downloadRuleTemplate(HttpServletResponse res) throws IOException {
        excelSupport.downloadTemplate(res, ExcelConstants.IMPORT_TASK_TEMPLATE_COLUMNS,
                ExcelConstants.IMPORT_TASK_TEMPLATE_COMMENTS,
                ExcelConstants.IMPORT_TASK_TEMPLATE_SHEET_NAME,
                ExcelConstants.IMPORT_TASK_TEMPLATE_EXAMPLE,
                ExcelConstants.IMPORT_TASk_TEMPLATE_NAME);
    }

    @PostMapping("/upload")
    public ReturnVo updateRuleTemplate(@RequestBody MultipartFile file) throws IOException {
        easyExcelSupport.analysisTemplate(file.getInputStream(), 10, importTaskService, TaskTemplateDto.class);
        return ReturnVo.ok();
    }
}
